Newer
Older
taehui / taehui-fe / src / app / [language] / forum / query / usePostComment.ts
@Taehui Taehui on 17 Mar 857 bytes 2024-03-17 오후 2:12
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { wwwAPI } from "@/utilities/wwwAPI";
import { getMillis } from "taehui-ts/date";

import { useAvatarStore } from "@/store/Stores";

export default function usePostComment() {
  const { totem } = useAvatarStore();

  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: async ({
      essayID,
      targetCommentID,
      text,
    }: {
      essayID: string;
      targetCommentID: number;
      text: string;
    }) => {
      await wwwAPI.post(
        `/comment/${essayID}`,
        { targetCommentID, text },
        {
          headers: {
            millis: getMillis(),
            totem,
          },
        },
      );
    },
    onSuccess: async (data) => {
      await queryClient.invalidateQueries({ queryKey: ["comment"] });
    },
  });
}